package com.easy.work.function;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.easy.work.MainActivity;
import com.easy.work.R;
import com.easy.work.app.AppSession;
import com.easy.work.app.BaseActivity;
import com.easy.work.utils.DisplayUtil;

import java.util.ArrayList;

public class NavActivity extends BaseActivity {
    public static final String TAG = "NavActivity";
    private ViewPager mViewPager;
    private ArrayList<View> viewlist;
    private int pos = 0;
    private int maxPos = 0;
    NavActivity context = this;
    int currentPageScrollStatus;
    private ArrayList<View> dotViewList = new ArrayList<View>();
    View currentDotView = null;
    public static final boolean IS_LOCAL_DOT = true;//viewpager 的滑动是否使用自己绘的点
    boolean isShowNews = true;
    //    int[] navIds = {R.drawable.welcom1, R.drawable.welcom2};
    int[] navIds = {R.drawable.welcome_1,R.drawable.welcome_2};

    @Override
    public void onCreate(Bundle savedInstanceState) {
//        setTheme(R.style.NoTitleBarFullscreen);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.act_nav);
        mViewPager = (ViewPager) findViewById(R.id.viewpager1);
        viewlist = new ArrayList<View>();


        for (int i = 0; i < navIds.length; i++) {
            View itemFirst = getLayoutInflater().inflate(R.layout.nav_item_1, null);
            ImageView imageView = (ImageView) itemFirst.findViewById(R.id.img);
            imageView.setImageDrawable(getResources().getDrawable(navIds[i]));
//			if (i == navIds.length - 1)
//				imageView.setOnClickListener(new View.OnClickListener() {
//					@Override
//					public void onClick(View v) {
//						goHome();
//					}
//				});
            viewlist.add(itemFirst);
        }

        //最后一页  特殊处理
        addfinalItem();


        mViewPager.setAdapter(new MyPagerAdapter());
        mViewPager.setOnPageChangeListener(new mOnpagerChangeLister());
        maxPos = viewlist.size() - 1;

        LinearLayout dotlayout = (LinearLayout) findViewById(R.id.dotlayout);
        if (dotlayout != null) {
            if (IS_LOCAL_DOT)
                dotlayout.setVisibility(View.VISIBLE);
            else
                dotlayout.setVisibility(View.INVISIBLE);
            int width = DisplayUtil.dip2px(context, 10);
            int margin = DisplayUtil.dip2px(context, 5);
            for (int i = 0; i < viewlist.size(); i++) {
                TextView textView = new TextView(context);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, width);
                params.setMargins(margin, 0, margin, 0);
                textView.setLayoutParams(params);
                textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.nav_dot_bg));
                dotlayout.addView(textView);
                final int index = i;
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                dotViewList.add(textView);
            }
        }
        currentDotView = dotViewList.get(0);
        currentDotView.setSelected(true);
    }

    @Override
    public boolean isActivityFitsSystemWindows() {
        return false;
    }


    /**
     * 特别注意第二个版本发布的时候 需要判断是否登录 去掉登录按钮
     */
    void addfinalItem() {
        View nav_item_final = getLayoutInflater().inflate(R.layout.nav_item_final, null);

        final View eventView = nav_item_final.findViewById(R.id.btn_enter);
        if (eventView != null) {
            eventView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    eventView.setBackgroundColor(Color.parseColor("#7891ed"));
                    AppSession.getInstance().setNaved(true);
                    goHome();
                }
            });
        }


        viewlist.add(nav_item_final);
    }

    private class MyPagerAdapter extends PagerAdapter {

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView(viewlist.get(position));
        }

        @Override
        public int getCount() {
            return viewlist.size();
        }

        @Override
        public Object instantiateItem(View container, int position) {
            View view = viewlist.get(position);

            if (position == viewlist.size() - 1) {
//                View btn_login = view.findViewById(R.id.btn_login);
//                if (btn_login != null) {
//                    if (new UserInfoDao(context).isLogin()) {
//                        btn_login.setVisibility(View.GONE);
//                    }
//                }
            }

            ((ViewPager) container).addView(view);
            return viewlist.get(position);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

    }

    private class mOnpagerChangeLister implements
            ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
            currentPageScrollStatus = arg0;

        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
            if (pos == maxPos) {
//				AppSetting.getInstance(context).setTureNavi(true);//到最后一页 认为已经使用完导航页
                // 已经在最后一页还想往右划
//                if (positionOffsetPixels == 0 && currentPageScrollStatus == 1) {
//                    goHome();
//                }
            }

        }

        @Override
        public void onPageSelected(int arg0) {
            pos = arg0;
            if (arg0 < dotViewList.size()) {
                currentDotView.setSelected(false);
                currentDotView = dotViewList.get(arg0);
                currentDotView.setSelected(true);
            }

        }

    }


    void goHome() {
        AppSession.getInstance().getNaved();
        LoginActivity.start(this);
        finish();
    }
}
